<?php
/**
 * @description:
 * @author     : Geomen
 * @time       : 2019/1/23 15:56
 */


namespace App\Http\Controllers;


use Carbon\Carbon;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    /**
     * 登录页面
     *
     * @param Request $request
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request->isMethod('post')){
            $remember = $request->input('remember_me') == 'on' ? true : false;
            if (\Auth::attempt($request->only('username','password'),$remember)){
                $user = \Auth::user();
                if ($user->is_del){
                    \Auth::logout();
                    return back()->with('msg','该账号已被禁用,无法登录')->withInput($request->except('password'));
                }
                $user->last_login = Carbon::now();
                $user->save();
                return redirect()->intended('dashboard');
            }
            return back()->with('msg','用户名或密码错误')->withInput($request->except('password'));
        }
        return view('login');
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        \Auth::logout();
        return redirect('login');
    }
}